'''urllib_post爬取肯德基
 网址：http://www.kfc.com.cn/kfccda/storelist/index.aspx
需求：
    1.爬取肯德基-餐厅信息 前10页数据；
    2.将爬取到的数据，下载到本地；

分页逻辑：
    请求url：http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname
    请求体：
        第一页：{"cname": "北京","pageIndex": 1, "pageSize": 10}
        第二页：{"cname": "北京","pageIndex": 2, "pageSize": 10}
        第三页：{"cname": "北京","pageIndex": 3, "pageSize": 10}
 '''

import urllib.request
import urllib.parse

# 定制肯德基POST请求Request
def create_request(pageNum):
    kfc_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
    kfc_data = {
        "cname": "北京",
        "pageIndex": pageNum,
        "pageSize": 10
    }
    # post请求体必须编码
    kfc_data = urllib.parse.urlencode(kfc_data).encode('utf-8')
    # print(kfc_data)
    kfc_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
    }
    request = urllib.request.Request(url=kfc_url, headers=kfc_headers, data=kfc_data)
    return request

# 获取肯德基POST 相应体
def gain_response(request):
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    # print(content)
    return content

# 将指定文本下载到本地
def down_file(fileFile, fileContent):
    with open(fileFile, 'w', encoding='utf-8') as fp:
        fp.write(fileContent)

if __name__ == '__main__':
    start_num = int(input('请输入起始页：'))
    end_num = int(input('请输入终止页：'))
    for i in range(start_num, end_num + 1):
        request = create_request(i)
        content = gain_response(request)
        log_title = '爬取肯德基第{0}页'.format(i)
        print(log_title, content)
        filePath = 'D:\\360极速浏览器下载\\'+log_title +'.json'
        down_file(filePath, content)
